package it.xiuxian.lib.view;

import android.app.Activity;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.bottomsheet.BottomSheetDialog;

import java.util.ArrayList;

import it.xiuxian.lib.R;
import it.xiuxian.lib.bean.ItemBean;
import it.xiuxian.lib.utils.adapter.SlimAdapter;
import it.xiuxian.lib.utils.adapter.SlimInjector;
import it.xiuxian.lib.utils.adapter.injector.IViewInjector;

public class BottomSheetUtils {
    @RequiresApi(api = Build.VERSION_CODES.M)
    public  BottomSheetDialog getBottomSheet(Activity activity, String title, OnSure sure, ArrayList<ItemBean> ItemBeans) {
       BottomSheetDialog bottomSheetDialog= new BottomSheetDialog(activity);
        View inflate = activity.getLayoutInflater().inflate(R.layout.lib_bottom_sheet_selector, null, false);
        TextView tvTitle = inflate.findViewById(R.id.tvTitle);
        tvTitle.setTextColor(activity.getColor(R.color.libTextGray));
        TextView tvCancel = inflate.findViewById(R.id.tvCancel);
        RecyclerView recyclerView = inflate.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        SlimAdapter.create().register(R.layout.lib_recy_item_text, new SlimInjector<ItemBean>() {
            @Override
            public void onInject(ItemBean data, IViewInjector injector) {
                injector.text(R.id.tv_content,data.content);
                TextView tv = (TextView)injector.findViewById(R.id.tv_content);
                tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v){
                       sure.onSure(ItemBeans.indexOf(data));
                       bottomSheetDialog.dismiss();
                    }
                });
            }
        }).attachTo(recyclerView).updateData(ItemBeans);
        if (TextUtils.isEmpty(title)) {
            tvTitle.setVisibility(View.GONE);
        } else {
            tvTitle.setVisibility(View.VISIBLE);
            tvTitle.setText(title);
        }
        tvCancel.setOnClickListener(v -> {
            bottomSheetDialog.dismiss();
        });
        bottomSheetDialog.setContentView(inflate);
        bottomSheetDialog.setCancelable(true);
        bottomSheetDialog.setCanceledOnTouchOutside(true);
        return  bottomSheetDialog;
    }

    public interface OnSure{
        void onSure(int i);
    }

}